﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MJM.SCA.EntidadesNegocio;
using System.Collections.Generic;
using MJM.RPP.EntidadesNegocio;
using MJM.Principal;
using MJM.SPP.EntidadesNegocio;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;

namespace wRegPadron.frmOpciones
{
    public partial class ucConsultaLog : System.Web.UI.UserControl
    {
        static public List<LogwsEN> lstLogwsEN = new List<LogwsEN>();
        public static PersonaE objPersonaE;
        public static string FechaInicio, FechaFin;

        public void SetPersona(PersonaE obj)
        {
            objPersonaE = obj;
        }

        protected String CadenaMes(int num)
        {
            switch (num)
            {
                case 1: return "Enero";
                case 2: return "Febrero";
                case 3: return "Marzo";
                case 4: return "Abril";
                case 5: return "Mayo";
                case 6: return "Junio";
                case 7: return "Julio";
                case 8: return "Agosto";
                case 9: return "Septiembre";
                case 10: return "Octubre";
                case 11: return "Noviembre";
                case 12: return "Diciembre";                
            }
            return "";
        }

        protected void CargarComboMes(int Anio)
        {
            int mesmax = 12;
            if (Anio == DateTime.Now.Year)
            {
                mesmax = DateTime.Now.Month;
            }
            List<GraficoEN> lst = new List<GraficoEN>();
            GraficoEN obj;
            for (int i = 0; i < mesmax; i++)
            {
                obj = new GraficoEN();
                obj.Cantidad = i + 1;
                obj.Texto = CadenaMes(i + 1);
                lst.Add(obj);
            }
            ddlMes.DataSource = lst;
            ddlMes.DataTextField = "Texto";
            ddlMes.DataValueField = "Cantidad";
            ddlMes.DataBind();
        }

        protected List<GraficoEN> ObtenerSerie(List<GraficoEN> lst, int i)
        {
            List<GraficoEN> aux = new List<GraficoEN>();
            aux.Add(lst[i]);
            return aux;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            int anio = DateTime.Now.Year;
            int dif=0;
            if (!IsPostBack)
            {
                dif = anio - 2012 + 1;
                ddlWebService.DataSource = MediadorCN.GetGrafico().ListarWSddl();
                ddlWebService.DataTextField = "Texto";
                ddlWebService.DataValueField = "Texto";
                ddlWebService.DataBind();
                for (int i = 0; i < dif; i++)
                    ddlAnio.Items.Add((anio - i).ToString());
                ddlAnio.SelectedValue = DateTime.Now.Year.ToString();
                CargarComboMes(DateTime.Now.Year);
                SubOpcionEN objSubOpcionEN = MediadorCN.GetSubOpcion().ObtenerTexto("MenuLog");
                lblTitulo.Text = objSubOpcionEN.SUBOPT_chNOMSOP;
                lblDescripcion.Text = objSubOpcionEN.SUBOPT_chDESSOP;
            }
            lstLogwsEN = MediadorCN.GetLogws().ListarLog("", "", "", "", "", "");
            gvLog.DataSource = lstLogwsEN;
            gvLog.DataBind();

            
        }

        protected void btnActualizar_Click(object sender, EventArgs e)
        {
            lstLogwsEN = MediadorCN.GetLogws().ListarLog("", "", "", "", "", "");
            gvLog.DataSource = lstLogwsEN;
            gvLog.DataBind();
        }

        protected void gvLog_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvLog.PageIndex = e.NewPageIndex;
            gvLog.DataSource = lstLogwsEN;
            gvLog.DataBind();
        }

        protected void btnAceptar_Click(object sender, EventArgs e)
        {
            List<GraficoEN> lstDatos = new List<GraficoEN>();
            List<GraficoEN> lst = new List<GraficoEN>();
            Series miSerie = new Series();
            if (ddlWebService.SelectedItem.Text == "Todos")
            {
                lstDatos = MediadorCN.GetGrafico().TotalAccesosWSxMes(Convert.ToInt32(ddlMes.SelectedValue), ddlAnio.SelectedItem.Text);
                gvPorcentajeAccWS.DataSource = lstDatos;
                gvPorcentajeAccWS.DataBind();

                List<GraficoEN> lstWService = MediadorCN.GetGrafico().ListarWS();
                for (int i = 0; i < lstWService.Count; i++)
                {
                    lst = MediadorCN.GetGrafico().TotalAccesosDiarioWS(ddlAnio.SelectedItem.Text, Convert.ToInt32(ddlMes.SelectedValue), lstWService[i].Texto);
                    miSerie = new Series();
                    miSerie["PieLabelStyle"] = "InSide";
                    miSerie.IsValueShownAsLabel = false;
                    miSerie.Name = lstWService[i].Texto;
                    miSerie.Points.DataBind(lst, "Texto", "Cantidad", "");
                    miSerie.ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Area;
                    Grafico2.Series.Add(miSerie);
                }
            }
            else
            {
                lstDatos = MediadorCN.GetGrafico().TotalMetxMes(ddlAnio.SelectedItem.Text, Convert.ToInt32(ddlMes.SelectedValue), ddlWebService.SelectedItem.Text);
                gvPorcentajeAccWS.DataSource = lstDatos;
                gvPorcentajeAccWS.DataBind();
                
                for (int i = 0; i < lstDatos.Count; i++)
                {
                    lst = MediadorCN.GetGrafico().TotalAccesosDiarioMet(ddlAnio.SelectedItem.Text, Convert.ToInt32(ddlMes.SelectedValue), lstDatos[i].Texto);
                    miSerie = new Series();
                    miSerie["PieLabelStyle"] = "InSide";
                    miSerie.IsValueShownAsLabel = false;
                    miSerie.Name = lstDatos[i].Texto;
                    miSerie.Points.DataBind(lst, "Texto", "Cantidad", "");
                    miSerie.ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Area;                    
                    Grafico2.Series.Add(miSerie);
                }
            }
            miSerie = new Series();
            miSerie.Name = "Total de accesos";
            miSerie["PieLabelStyle"] = "InSide";
            miSerie.IsValueShownAsLabel = true;
            miSerie.ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Pie;
            miSerie.BackGradientStyle = GradientStyle.DiagonalLeft;
            miSerie.BackSecondaryColor = System.Drawing.Color.LightGray;
            miSerie.Points.DataBind(lstDatos, "Texto", "Cantidad", "");
            miSerie["PieLineColor"] = "Black";
            miSerie["PieDrawingStyle"] = "Concave";
            miSerie.Font = new Font("Arial", 8);
            Grafico1.Series.Add(miSerie);
            Grafico1.ChartAreas[0].Position.Auto = false;
            Grafico1.ChartAreas[0].Position.X = 5;
            Grafico1.ChartAreas[0].Position.Y = 25;
            Grafico1.ChartAreas[0].Position.Width = 70;
            Grafico1.ChartAreas[0].Position.Height = 70;
            Grafico1.ChartAreas[0].AxisX.IsLabelAutoFit = true;
            Grafico1.ChartAreas[0].AxisX.Interval = 1;
            Grafico1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
            Grafico1.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
            Grafico1.ChartAreas[0].AxisY.LabelStyle.Font = new Font("Arial", 8);
            Grafico1.ChartAreas[0].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.None;
            Grafico1.Titles[0].Font = new Font("Arial", 12, FontStyle.Bold);
            Grafico1.Titles[0].ForeColor = Color.Black;

            Grafico2.ChartAreas[0].Position.Auto = false;
            Grafico2.ChartAreas[0].Position.X = 5;
            Grafico2.ChartAreas[0].Position.Y = 15;
            Grafico2.ChartAreas[0].Position.Width = 90;
            if(lstDatos.Count>8)
                Grafico2.ChartAreas[0].Position.Height = 60;
            else if (lstDatos.Count > 4)
                Grafico2.ChartAreas[0].Position.Height = 65;
            else
                Grafico2.ChartAreas[0].Position.Height = 70;
            Grafico2.ChartAreas[0].AxisX.IsLabelAutoFit = true;
            Grafico2.ChartAreas[0].AxisX.Interval = 1;
            Grafico2.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
            Grafico2.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
            Grafico2.ChartAreas[0].AxisY.LabelStyle.Font = new Font("Arial", 10);
            Grafico2.ChartAreas[0].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.None;            
            Grafico2.Titles[0].Font = new Font("Trebuchet MS", 14, FontStyle.Bold);
            Grafico2.Titles[0].ForeColor = Color.Black;
            Grafico2.Legends[0].ItemColumnSeparator = LegendSeparatorStyle.DashLine;            
            Grafico2.Legends[0].TableStyle=LegendTableStyle.Wide;            
        }

        protected void ddlAnio_SelectedIndexChanged(object sender, EventArgs e)
        {
            CargarComboMes(Convert.ToInt32(ddlAnio.SelectedItem.Text));
        }

    }
}